/**
 * Copyright (c) 2012, Camelot and/or its affiliates. All rights reserved.
 * Camelot PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.camelot.ccr.system.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.camelot.ccr.global.controller.BaseController;
import com.camelot.ccr.global.util.Pager;
import com.camelot.ccr.global.util.Utility;
import com.camelot.ccr.system.dao.DriverDao;
import com.camelot.ccr.system.dao.MatchTeamDao;
import com.camelot.ccr.system.dao.MatchTypeDao;
import com.camelot.ccr.system.dao.PictureDao;
import com.camelot.ccr.system.dao.SponsorDao;
import com.camelot.ccr.system.dao.SponsorTeamDao;
import com.camelot.ccr.system.dao.TeamDao;
import com.camelot.ccr.system.enums.ModuleEnum;
import com.camelot.ccr.system.persistence.MatchTeam;
import com.camelot.ccr.system.persistence.MatchType;
import com.camelot.ccr.system.persistence.Picture;
import com.camelot.ccr.system.persistence.Sponsor;
import com.camelot.ccr.system.persistence.SponsorTeam;
import com.camelot.ccr.system.persistence.Team;

/**
 * <pre>
 * 车队信息Controller
 * </pre>
 * @author Liu Jun
 * @version V0.1, 2014-5-23 下午2:11:38
 */
@Controller
@RequestMapping("/manage/team")
public class TeamController extends BaseController {

	@Resource
	private TeamDao teamDao;
	@Resource
	private DriverDao driverDao;
	@Resource
	private PictureDao pictureDao;
	@Resource
	private MatchTeamDao matchTeamDao;
	@Resource
	private MatchTypeDao matchTypeDao;
	@Resource
	private SponsorDao sponsorDao;
	@Resource
	private SponsorTeamDao sponsorTeamDao;
	/**
	 * 车队图标存放地址
	 */
	public static final String teamHeadUrlPath="/upload/images/teamheadphoto/";
	/**
	 * 车队的图文资讯
	 */
	public static final String teamTwzxPath = "/upload/images/teamtwzx";
	/**
	 * <pre>
	 * 查询车队信息
	 * </pre>
	 * @param matchTeam
	 * @param model
	 * @return 车队列表
	 * @author Liu Jun, 2014-5-23 下午5:36:47
	 */
	@RequestMapping("/list")
	public String list(MatchTeam matchTeam,ModelMap model){
		List<MatchTeam> list = matchTeamDao.findByExample(matchTeam);
		
		model.put("list", list);
		return "/manage/matchteam/team_index";
	}
	/**
	 * <pre>
	 * <跳转到车手添加页面>
	 * </pre>
	 * @param driver
	 * @param model
	 * @return
	 * @author Liu Jun, 2014-5-23 下午11:22:23
	 */
	@RequestMapping("/add")
	public String add(MatchType matchType,ModelMap model){
		List<MatchType> matchTypeList = matchTypeDao.getAll();
		model.put("matchTypeList", matchTypeList);
		return "/manage/matchteam/team_add";
	}
	
	/**
	 * <pre>
	 * <保存车队信息和赛事类型并上传头像>
	 * </pre>
	 * @param file
	 * @param request
	 * @return
	 * @throws Exception
	 * @author Liu Jun, 2014-5-24 上午8:10:52
	 */
	@RequestMapping("/save")
	public String save(@RequestParam("teamPhotoUp")CommonsMultipartFile file1,
			@RequestParam("teamPhotoDown")CommonsMultipartFile file2,HttpServletRequest request) throws Exception{
		//获取车队信息
		String teamName = request.getParameter("teamName");
		String firstMatchTime = request.getParameter("firstMatchTime");
		String address = request.getParameter("address");
		String matchTime = request.getParameter("matchTime");
		String teamChampion = request.getParameter("teamChampion");
		String driverChampion = request.getParameter("driverChampion");
		String point = request.getParameter("point");
		String firstDriver = request.getParameter("firstDriver");
		String secondDriver = request.getParameter("secondDriver");
		String testDriver = request.getParameter("testDriver");
		String teamUrl = request.getParameter("teamUrl");
		String[] matchType=request.getParameterValues("matchType");
		
		String path = request.getRealPath(teamHeadUrlPath);
		
		Team t = new  Team();
		t.setTeamName(teamName);
		t.setFirstMatchTime(Utility.convertStringToTimestamp(firstMatchTime, Utility.DATE_DEFAULT_PATTERN));
		t.setAddress(address);
		t.setMatchTime(Integer.parseInt(matchTime));
		t.setTeamChampion(Integer.parseInt(teamChampion));
		t.setDriverChampion(Integer.parseInt(teamChampion));
		t.setPoint(Double.parseDouble(point));
		t.setFirstDriver(firstDriver);
		t.setSecondDriver(secondDriver);
		t.setTestDriver(testDriver);
		t.setTeamUrl(teamUrl);
		t.setDriverChampion(Integer.valueOf(driverChampion));
		t.setTeamPhotoUp(path);
		t.setTeamPhotoDown(path);
		t.setCreateTime(new Date());
		
		
		teamDao.saveTeam(t, file1,file2,matchType);
		System.out.println(t);
		
		return "redirect:list";
	}

	/**
	 * <pre>
	 * <车队图文资讯>
	 * </pre>
	 * @param teamId
	 * @param model
	 * @param request
	 * @return  车队图文资讯列表
	 * @author Liu Jun, 2014-5-24 下午1:38:13
	 */
	@RequestMapping("/twzx")
	public String twzx(String teamId,ModelMap model,HttpServletRequest request){
		//ToDo 暂时定100
		String curpage = request.getParameter("curpage")==null?"0":request.getParameter("curpage");
		String pageSize = request.getParameter("pageSize");//ToDo 暂时未用到  暂定每页显示100
		//设置参数
		Picture example = new Picture();
		example.setBusinessId(teamId);
		example.setMoudleType(ModuleEnum.TEAM_TWZX.name());
		Pager<Picture> pager = pictureDao.findPageList(Integer.parseInt(curpage), 100, example);
		
		model.put("pager", pager);
		model.put("teamId", teamId);
		return "/manage/matchteam/team_twzx";
	}
	/**
	 * <pre>
	 * <uploadify组件图文资讯批量上传>
	 * </pre>
	 * @param request
	 * @throws Exception
	 * @author Liu Jun, 2014-5-24 上午10:25:43
	 */
	@RequestMapping("/uploadTwzx")
	public String uploadTwzx(MultipartHttpServletRequest request) throws Exception{
		
		String businessId = request.getParameter("businessId");
		String path = request.getRealPath(teamTwzxPath);
		List<MultipartFile> files = request.getFiles("files"); 
		String imageUrl = "";
		if(Utility.isNotEmpty(files)){
			Picture p = null;
			for(int i=0;i<files.size();i++){
				imageUrl = Utility.uploadFile(files.get(i), path,teamTwzxPath);
				p = new Picture();
				p.setBusinessId(businessId);
				p.setMoudleType(ModuleEnum.TEAM_TWZX.name());
				p.setImgUrl(imageUrl);
				pictureDao.save(p);
			}
		}
		return "redirect:twzx?teamId="+businessId;
	}
	/**
	 * <pre>
	 * <配置赞助商>
	 * </pre>
	 * @param example
	 * @param model
	 * @return
	 * @author Liu Jun, 2014-5-24 下午1:59:57
	 */
	@RequestMapping("/configSponsor")
	public String configSponsor(SponsorTeam example,ModelMap model){
		List<Sponsor> sponsorlList = sponsorDao.getAll();
		List<SponsorTeam> sponsorTeamList = sponsorTeamDao.findByExample(example);
		Team team = teamDao.get(example.getTeamId());
		model.put("sponsorlList", sponsorlList);
		model.put("sponsorTeamList", sponsorTeamList);
		model.put("team", team);
		return "/manage/matchteam/team_sponsor";
	}
	/**
	 * <pre>
	 * <保存车队和赞助商关系>
	 * </pre>
	 * @param sponsorIds
	 * @param teamId
	 * @return
	 * @author Liu Jun, 2014-5-24 下午2:25:16
	 */
	@RequestMapping("/saveSponsorTeam")
	public String saveSponsorTeam(String[] sponsorIds,String teamId){
		sponsorTeamDao.saveBatchSponsorTeam(sponsorIds, teamId);
		return "redirect:list";
	}
	
	@RequestMapping("/detail")
	public String detail(ModelMap model,HttpServletRequest request) throws Exception{
		String id = request.getParameter("id");
		Team team = teamDao.findById(id);
		List<MatchTeam> matchTeamList = matchTeamDao.findByTeam(team);
		
		model.put("team", team);
		model.put("matchTeamList", matchTeamList);
		return "/manage/matchteam/team_detail";
	}
}
